package org.gov.vis.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.master.dto.GopalaMithraDto;

public class VISDataGrid {
	public static final Color COLOR_1 = new Color(220,234,238); 
    public static final Color COLOR_2 = new Color(247,229,232); 
    public static final Color COLOR_3 = new Color(255,255,232); 
	
	private static Object[][] getDataArrayFromList(List<Object> dataList, int cols){
		
		// gives number of rows
		int rowCount = dataList.size(); 
		
		// here give the number of columns you want to display on the screen
		int columnCount = cols;
		
		List<Object> rowList= null; // get firstObject from DataList
		
		/*
		 * Create a new Two dimensional array with rowCount
		 * and column count
		 */
		Object[][] dataArr=new Object[rowCount][columnCount]; 
		for(int i=0;i<rowCount;i++){
			rowList=(List<Object>)dataList.get(i);
			for(int j=0;j<columnCount;j++){
				dataArr[i][j]=rowList.get(j); //setting data
			}// end of columcount loop
		}// end of rowcount loop
		return dataArr;
	}
	
	private static JPanel getResultsPanel(){
		JPanel resultsPanel = new JPanel();
		
		GridBagLayout jPanel1Layout = new GridBagLayout();
		jPanel1Layout.rowWeights = new double[] {0.0};
		jPanel1Layout.rowHeights = new int[] {7};
		jPanel1Layout.columnWeights = new double[] {0.0};
		jPanel1Layout.columnWidths = new int[] {7};
		resultsPanel.setLayout(jPanel1Layout);
		
		return resultsPanel;
	}
	
	public static void createDataPanel(List<Object> dataList, List<String> colLst, JPanel parentPanel, ActionListener actionListener){
		
		parentPanel.removeAll();
		parentPanel.revalidate();
		parentPanel.repaint();
		
		JPanel resultsPanel = getResultsPanel();
		
		int cols = colLst.size();
		
		for (int i = 0; i < colLst.size(); i++) {
			JLabel msgLabel=new JLabel(colLst.get(i));
			msgLabel.setOpaque(true);
			msgLabel.setBackground(COLOR_1);
			resultsPanel.add(msgLabel, new GridBagConstraints(i, 0, 1, 1, 0.1, 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 2, 2, 0), 0, 0));
		}
		
		Object[][] abc = getDataArrayFromList(dataList, cols);
		for (int i = 0; i < abc.length; i++) {
			List<Object> rowList = (List<Object>)dataList.get(i);
			for(int j=0;j<=cols;j++){

				if(j==1){
					VISHyperLink msgLabel=new VISHyperLink(rowList.get(j).toString());
					msgLabel.setText("<html> <table><tr><td style='text-align:center;width:10px;height:15px;'>"+rowList.get(j).toString()+"</tr></td></table></html>");
					msgLabel.addActionListener(actionListener);
					msgLabel.setObject(rowList.get(j-1));
					msgLabel.setOpaque(true);
					msgLabel.setBackground(Color.WHITE);
					resultsPanel.add(msgLabel, new GridBagConstraints(j-1, i+1, 1, 1, 0.1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 2, 2, 0), 0, 0));
				
				}else if(j!=0){
					
				Object cellData = rowList.get(j);
				if(cellData==null){
					cellData = "NA";
				}
				
				JLabel msgLabel=new JLabel();
				msgLabel.setText("<html> <table><tr><td style='text-align:center;width:10px;height:15px;'>"+cellData.toString()+"</tr></td></table></html>");
				msgLabel.setOpaque(true);
				msgLabel.setBackground(Color.WHITE);
				//System.out.println(j-1);
				resultsPanel.add(msgLabel, new GridBagConstraints(j-1, i+1, 1, 1, 0.1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 2, 2, 0), 0, 0));
				}
			}
		}
		parentPanel.setBackground(Color.WHITE);
		parentPanel.add(resultsPanel,new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

		//parentPanel.add(resultsPanel);
	}
	
	public static List<String> getColLst(){
		List<String> colLst = new ArrayList<String>();
		colLst.add("Id");
		colLst.add("District");
		colLst.add("Code");
		colLst.add("Name");

		return colLst;
	}
	
	public static List<GopalaMithraDto> getGpalMithra(){
		GopalaMithraDto gplMithra = new GopalaMithraDto();
		gplMithra.setName("GPLMithra1");
		gplMithra.setDistrict("District1");
		gplMithra.setCode("C001");
		gplMithra.setGplMthId(1);
		
		GopalaMithraDto gplMithra1 = new GopalaMithraDto();
		gplMithra1.setName("GPLMithra2");
		gplMithra1.setDistrict("District2");
		gplMithra1.setCode("C002");
		gplMithra1.setGplMthId(2);
		
		
		GopalaMithraDto gplMithra2 = new GopalaMithraDto();
		gplMithra2.setName("GPLMithra3");
		gplMithra2.setDistrict("District3");
		gplMithra2.setCode("C003");
		gplMithra2.setGplMthId(3);
		
		
		GopalaMithraDto gplMithra3 = new GopalaMithraDto();
		gplMithra3.setName("GPLMithra4");
		gplMithra3.setDistrict("District4");
		gplMithra3.setCode("C004");
		gplMithra3.setGplMthId(4);
		
		List<GopalaMithraDto> lstGplMit = new ArrayList<GopalaMithraDto>();
		lstGplMit.add(gplMithra);
		lstGplMit.add(gplMithra1);
		lstGplMit.add(gplMithra2);
		lstGplMit.add(gplMithra3);
		return lstGplMit;
	}
	
	public static void main(String args[]){
		
		
		
		
		/*ArrayList<Object> dataList = new ArrayList<Object>(1);
		if(lstGplMit != null && !lstGplMit.isEmpty()){
			for(GopalaMithraDto dto:lstGplMit){
				ArrayList<Object> lst = new ArrayList<Object>(1);
				//setting each property of the current Accessory object (row) to the list
				lst.add(dto);
				lst.add(dto.getGplMthId());
				lst.add(dto.getCode());
				lst.add(dto.getName());
				lst.add(dto.getDistrict());
				dataList.add(lst);
			}//end for 
		}//end if
*/		
		//createDataPanel(dataList,null);
		
	}
	
}
